package com.sdlh.demo.concurrent.structure;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.StructuredTaskScope;

@Slf4j
public class StructuredTaskScopeDemo {
    public static void main(String[] args) {
        try(var scope = new StructuredTaskScope.ShutdownOnFailure()) {
            // 启动多个并发任务
            StructuredTaskScope.Subtask<String> task1 = scope.fork(() -> {
                // 执行任务
                System.out.println("Task 1 is running");
                throw new RuntimeException("Task 1 ecounter error");
            });
            StructuredTaskScope.Subtask<String> task2 = scope.fork(() -> {
                // 执行任务
                Thread.sleep(500);
                System.out.println("Task 2 is running");
                return null;
            });

            // 等待所有任务完成
            scope.join();
            scope.throwIfFailed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
